//import java.awt.*;
import javax.swing.*;
//import javax.swing.GroupLayout.Alignment;
//import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class usernovo extends JDialog{
	private JTextField txtDigiteSeuNome;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField txtDigiteSuaIdade;
	//String sexoUser = "";
	private JPasswordField passwordField;
	public usernovo() {
		setResizable(false);
		
		setTitle("Cadastro de usu\u00E1rio");
		
		JLabel lblNome = new JLabel("Nome:");
		
		JLabel lblIdade = new JLabel("Idade:");
		
		JLabel lblSexo = new JLabel("Sexo:");
		
		JLabel lblSenha = new JLabel("Senha:");
		
		txtDigiteSuaIdade = new JTextField();
		txtDigiteSuaIdade.setToolTipText("Campo da Idade");
		txtDigiteSuaIdade.setColumns(10);
		
		txtDigiteSeuNome = new JTextField();
		txtDigiteSeuNome.setToolTipText("Campo do Usuário");
		txtDigiteSeuNome.setColumns(10);
		txtDigiteSeuNome.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setToolTipText("Campo senha");
		
		JButton btnLimpar = new JButton("Limpar");
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtDigiteSeuNome.setText("");
				txtDigiteSuaIdade.setText("");
				passwordField.setText("");
			}
		});
		
		JRadioButton rdbtnM = new JRadioButton("M");
		rdbtnM.setToolTipText("Campo do Sexo");
		rdbtnM.setSelected(true);
		buttonGroup.add(rdbtnM);
		rdbtnM.setActionCommand("Masculino");
		
		JRadioButton rdbtnF = new JRadioButton("F");
		rdbtnF.setToolTipText("Campo do Sexo");
		//rdbtnF.setSelected(false);
		buttonGroup.add(rdbtnF);
		rdbtnF.setActionCommand("Feminino");
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String getNome = txtDigiteSeuNome.getText();
				String getIdade = txtDigiteSuaIdade.getText();
				String getSenha = passwordField.getText();
				File file = new File ("C:/Eclipse (Juno x86)/workspace/testes/arqs/fileusuario.txt");
				try{
					FileWriter writer = new FileWriter(file,true);
					BufferedWriter buffer = new BufferedWriter(writer);
					buffer.write("Gravar nome: "+getNome);
					buffer.newLine();//quebra de linha
					buffer.write("Gravar Idade: "+getIdade);
					buffer.newLine();//quebra de linha
					buffer.write("Gravar Sexo: "+buttonGroup.getSelection().getActionCommand());
					buffer.newLine();//quebra de linha
					buffer.write("Gravar Senha: "+getSenha);
					buffer.newLine();//quebra de linha
					buffer.flush();
					buffer.close();
				} catch (IOException o){
					o.printStackTrace();
				}
				usernovo.this.dispose();
			}

		});

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblIdade, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addGap(18))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNome)
									.addPreferredGap(ComponentPlacement.RELATED)))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txtDigiteSeuNome, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(rdbtnM)
									.addGap(11)
									.addComponent(rdbtnF))
								.addComponent(txtDigiteSuaIdade, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
							.addGap(31))
						.addComponent(lblSexo, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(10)
							.addComponent(btnSalvar)
							.addGap(18)
							.addComponent(btnLimpar)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblSenha)
							.addGap(16)
							.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
							.addGap(67))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtDigiteSeuNome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNome))
							.addGap(41)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblIdade)
								.addComponent(txtDigiteSuaIdade, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(42)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblSexo, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
								.addComponent(rdbtnM)
								.addComponent(rdbtnF))))
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSenha)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSalvar)
						.addComponent(btnLimpar))
					.addContainerGap(17, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
	public static void main(String[] args){
		
        usernovo exemplo = new usernovo();
        exemplo.setSize(205, 200);
        exemplo.setResizable(false);//Travar a redimensão.
        exemplo.setVisible(true);
	}
}
